package com.woniu.microworkorderserver.businessmag.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.woniu.microcommonsserver.entity.*;
import com.woniu.microcommonsserver.param.PageRequest;
import com.woniu.microcommonsserver.param.PageResult;
import com.woniu.microcommonsserver.param.ResponseMsg;
import com.woniu.microworkorderserver.businessmag.service.InspectPlanService;
import com.woniu.microworkorderserver.util.JsonToObject;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/inspectPlan")
public class InspectPlanController {

    @Resource
    private InspectPlanService inspectPlanServiceImpl;

    /**
     * 新增数据
     * */
    @PostMapping(value = "/save",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseMsg save(@RequestBody JSONObject jsonObject){
        System.out.println(JsonToObject.getPollingPlans(jsonObject));
        System.out.println(JsonToObject.getInspectPoints(jsonObject));
        inspectPlanServiceImpl.save(JsonToObject.getPollingPlans(jsonObject).get(0),
                JsonToObject.getInspectPoints(jsonObject).get(0));
        return ResponseMsg.builder().code(200).msg("新增成功").build();
    }

    /**
     * 分页查询
     * */
    @GetMapping(value = "/page",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseMsg findByPage(PageRequest request,PollingPlanInfoEntity entity){
        System.out.println(entity);
        System.out.println(request);
        PageInfo<PollingPlanInfoEntity> page = inspectPlanServiceImpl.findPage(request, entity);
        PageResult<PollingPlanInfoEntity> result = new PageResult<>();
        result.setTotal(page.getTotal());
        result.setRows(page.getList());
        return ResponseMsg.builder().code(200).msg("查询成功").data(result).build();
    }

    /**
     * 删除数据
     * */
    @GetMapping(value = "/delete",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseMsg delete(long inspectPlanId){
        System.out.println(inspectPlanId);
        inspectPlanServiceImpl.delete(inspectPlanId);
        return ResponseMsg.builder().code(200).msg("删除成功").build();
    }
}
